/**
 * 选区事件
 */
import Editor from "../editor"
import { SelectionModel } from '../models'
import { debounce } from "../utils/debounce"

export class SelectionChange {
    private isComposing: boolean = false
    constructor(private editor: Editor, private container: HTMLElement, private selectionModel: SelectionModel) {
        this.editor = editor
        this.container = container
        this.isComposing = false

    }


    selectionChangeHandler_() {
        if (this.isComposing) return
        const domSelection = window.getSelection()
        let selection = null
        if (domSelection) {
            selection = { anchor: domSelection.anchorOffset, focus: domSelection.focusOffset }
            this.editor.setSelection(selection.anchor, selection.focus);
        }
    }

    addListeners() {
        const domSelectionChangeHandelBinder_ = debounce(this.selectionChangeHandler_.bind(this), 50)
        window.document.addEventListener('selectionchange', domSelectionChangeHandelBinder_)
    }
}